package com.plian.system.task.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.entity.sys.SystemRecord;
import com.plian.system.service.sys.system.ISystemRecordService;
import com.plian.system.task.bank.BankTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 制度上报失效
 * @author: gangan
 * @create: 2020-08-29 10:48
 */
@Configuration
public class SystemRecordTask {
    private static final Logger log = LoggerFactory.getLogger(BankTask.class);

    @Autowired
    private ISystemRecordService systemRecordService;

    @Scheduled(cron = "0 0 1 * * ?")
    private void configureTasks() {
        log.info("制度上报失效 开始--------------> " + LocalDateTime.now());
        List<SystemRecord> systemRecords = systemRecordService.list(new QueryWrapper<SystemRecord>()
                .eq("system_status", 1).isNotNull("failure_time"));
        if (CollectionUtil.isNotEmpty(systemRecords)){
            long now = DateUtil.parse(DateUtil.format(DateUtil.now(), DateUtil.PATTERN_DATE), DateUtil.PATTERN_DATE).getTime();
            List<SystemRecord> updateList= new ArrayList<>();
            for (SystemRecord systemRecord :systemRecords){
                if (systemRecord.getFailureTime().getTime() <= now){
                    systemRecord.setSystemStatus(2);
                    updateList.add(systemRecord);
                    log.info("制度上报失效 --------------> 制度名称：" + systemRecord.getSystemName() + LocalDateTime.now());
                }
            }
            if (CollectionUtil.isNotEmpty(updateList)){
                systemRecordService.updateBatchById(updateList);
            }
        }

        log.info("制度上报失效 结束--------------> " + LocalDateTime.now());
    }
}
